package logic

import (
	"cart_srv/carl"
	"cart_srv/internal/svc"
	"cart_srv/mobile/mysql"
	"context"

	"github.com/zeromicro/go-zero/core/logx"
)

type CarlListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCarlListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CarlListLogic {
	return &CarlListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CarlListLogic) CarlList(in *carl.CarlListRequest) (*carl.CarlListResponse, error) {
	// todo: 列表

	c := mysql.Carl{}

	list, err := c.List(int(in.UserId))
	if err != nil {
		logx.Error("列表查询失败", err)
		return nil, err
	}

	var carlList []*carl.CarlItem

	for _, v := range list {
		carlList = append(carlList, &carl.CarlItem{
			GoodsId:    v.GoodId,
			GoodsTitle: v.GoodsTitle,
			GoodsPrice: float32(v.GoodsPrice),
			Num:        v.Num,
			UserId:     v.UserId,
			Checked:    v.Checked,
		})
	}

	return &carl.CarlListResponse{CarlList: carlList}, nil
}
